package com.hfzy.ihk.web.userCenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hfzy.ihk.common.util.Utils;
import com.hfzy.ihk.facade.userCenter.entity.Priv;
import com.hfzy.ihk.facade.userCenter.entity.UserHasCallLine;
import com.hfzy.ihk.facade.userCenter.entity.UserHasRole;
import com.hfzy.ihk.facade.userCenter.enums.CallLineType;
import com.hfzy.ihk.facade.userCenter.enums.CallType;
import com.hfzy.ihk.web.oauth.client.table.PageTableHandler;
import com.hfzy.ihk.web.oauth.client.table.PageTableRequest;
import com.hfzy.ihk.web.oauth.client.table.PageTableResponse;
import com.hfzy.ihk.web.userCenter.biz.UserBiz;
import com.hfzy.ihk.web.userCenter.biz.UserHasCallLineBiz;
import com.hfzy.ihk.web.userCenter.mapper.UserHasCallLineMapper;
import com.hfzy.ihk.web.userCenter.mapper.UserHasRoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
@RequestMapping("userHasCallLine")
@Slf4j
public class UserHasCallLineController {
    @Autowired
    UserHasCallLineMapper userHasCallLineMapper;

    @Autowired
    UserHasRoleMapper userHasRoleMapper;


    @Autowired
    UserHasCallLineBiz userHasCallLineBiz;

    @RequestMapping("queryUserHasCallLines")
    public ModelAndView queryUserHasCallLines(ModelAndView view){
        view.addObject("callTypes", CallType.values());
        view.addObject("callLineTypes", CallLineType.values());
        view.setViewName("page/callConfig/queryUserHasCallLines");
        return view;
    }

    @ResponseBody
    @RequestMapping("queryUserHasCallLinePage")
    public PageTableResponse queryUserHasCallLinePage(PageTableRequest request) {
        request.setOffset(request.getOffset()-1);
        return new PageTableHandler(new PageTableHandler.CountHandler() {
            @Override
            public int count(PageTableRequest request) {
                return userHasCallLineMapper.count(request.getParams());
            }
        }, new PageTableHandler.ListHandler() {
            @Override
            public List<UserHasCallLine> list(PageTableRequest request) {
                List<UserHasCallLine> list = userHasCallLineMapper.list(request.getParams(), request.getOffset(), request.getLimit());
                return list;
            }
        }).handle(request);
    }

    /**
     * 回显界面
     * */
    @RequestMapping("saveOrUpdateUserHasCallLines")
    public ModelAndView saveOrUpdateUserHasCallLines(ModelAndView view,boolean canEdit,int id){
        if(id!= 0){
           UserHasCallLine userHasCallLine = userHasCallLineMapper.selectJoinUsernameById(id);
           view.addObject("userHasCallLine",userHasCallLine);
           view.addObject("selCallLineTypeVal",userHasCallLine.getCallLineType().getValue());
        }
        view.addObject("callTypes",CallType.values());
        view.setViewName("page/callConfig/saveOrUpdateUserHasCallLines");
        return view;
    }

    /**
     * 保存
     * */
    @ResponseBody
    @RequestMapping("save")
    public Map<String,Object> save(@RequestParam Map<String,String> params){
        Map<String,Object> map = new HashMap<>();
        try{
            if(params.containsKey("callType") && params.containsKey("callLineType")
                    && !Utils.isNullString(params.get("callType")) && !Utils.isNullString(params.get("callLineType"))){
                CallType callType = CallType.fromValue(params.get("callType"));
                CallLineType callLineType = CallLineType.fromValue(params.get("callLineType"));
                if(params.containsKey("roleId") && !Utils.isNullString(params.get("roleId"))){
                    userHasCallLineBiz.saveUserHasCallLineByRole(Integer.parseInt(params.get("roleId")),callType,callLineType);
                }else if(params.containsKey("userId") && !Utils.isNullString(params.get("userId"))){
                    userHasCallLineBiz.saveUserHasCallLineByUserId(params.get("id"),Integer.parseInt(params.get("userId")),callType,callLineType);
                }
                map.put("code",1);
                map.put("msg","保存成功");
            }else{
                map.put("code",0);
                map.put("msg","保存失败，参数异常！");
            }
        }catch(IllegalArgumentException e1){
            map.put("code",0);
            map.put("msg","保存失败 "+ e1.getMessage());
        }catch (Exception e){
            log.error("保存失败{}","",e);
            map.put("code",0);
            map.put("msg","保存失败");
        }
        return map;
    }


    /**
     * 删除
     * */
    @RequestMapping("del")
    @ResponseBody
    public Map<String,Object> del(int id){
        Map<String,Object> map = new HashMap<>();
        try{
            int count =  userHasCallLineMapper.deleteById(id);
            map.put("code",1);
            map.put("msg","删除成功");
        }catch (Exception e){
            log.error("删除失败,{}",id,e);
            map.put("code",0);
            map.put("msg","删除失败");
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("getAllCallLineType")
    public List<Map> getAllCallLineType(){
        List<Map> mapList = new ArrayList<>();
        Arrays.asList(CallLineType.values()).forEach(
                it->{
                    Map map = new HashMap();
                    map.put("value",it.getValue());
                    map.put("desc",it.getDesc());
                    mapList.add(map);
                }
        );
        return mapList;
    }
}
